package com.mxin.ui.test

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mxin.common.log.LogA
import com.mxin.ui.R
import com.mxin.ui.base.BaseActivity
import com.mxin.ui.global.GlobalViewManager
import com.mxin.ui.list.BaseListViewHelper
import com.mxin.ui.list.DataSource
import com.mxin.ui.list.PageDataState
import com.mxin.ui.list.toPageData

class DirectoryActivity  : BaseActivity() {

    private val listViewHelper = BaseListViewHelper<Aty>()
    private lateinit var recyclerView: RecyclerView

    override fun setContentView() {
        setContentView(R.layout.ui_activity_test_aty)
    }

    override fun showLoadingView(): View?  = recyclerView

    override fun initView() {
        val contentView = findViewById<ViewGroup>(R.id.layout_content)
        recyclerView = listViewHelper.bindListView(contentView){
            it.layoutManager = LinearLayoutManager(this)
            it.adapter = AtyAdapter(mutableListOf()).apply {
                setOnItemClickListener { position , aty ->
                    when(position){
                        2 ->{
                            GlobalViewManager.createLoadingView(getContext()).show()
                        }
                        3 ->{
                            GlobalViewManager.createLoadingIndicatorView(getContext()).show()
                        }
                        else ->{
                            LogA.d( aty.descript)
                            startActivity(Intent(getContext(), aty.clazz))
                        }
                    }
                }
            }
        }
        listViewHelper.subscribeData {
            when{
                it.state== PageDataState.Null ->{
                    showLoadFailed()
                }
                else ->{
                    showLoadSuccess()
                }
            }
        }
    }

    override fun initData() {
        showLoading()
        //延迟3秒加载
        recyclerView.postDelayed({
            listViewHelper.emit(initListData().toPageData())
        }, 3000)
    }

    override fun onLoadRetry() {
        initData()
    }

    private fun initListData():List<Aty>{
        val data = mutableListOf<Aty>()
        data.add(Aty("没有分页的列表", TestListActivity::class.java))
        data.add(Aty("带刷新和分页的列表", TestRefreshListActivity::class.java))
        data.add(Aty("弹出普通LoadingDialog", this::class.java))
        data.add(Aty("弹出自定义LoadingDialog", this::class.java))
        data.add(Aty("设置全局初始化加载动画", LoadingViewConfigActivity::class.java))
        return data
    }

}

data class Aty(val descript:String, val clazz:Class<*>)

class AtyAdapter(val data:MutableList<Aty>) : RecyclerView.Adapter<AtyViewHolder>(), DataSource<Aty>, View.OnClickListener {

    private var onItemClickListener:((Int,Aty)->Unit)?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AtyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.ui_item_test_aty, parent, false)
        view.setOnClickListener(this)
        return AtyViewHolder(view)
    }

    override fun onBindViewHolder(holder: AtyViewHolder, position: Int) {
        holder.textView.text = data[position].descript
        holder.itemView.setTag(R.id.tag_holder, holder)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun addData(t: List<Aty>?) {
        val positionStart = data.size
        data.addAll(t?: emptyList())
        notifyItemRangeInserted(positionStart, data.size - positionStart)
    }

    override fun setData(t: List<Aty>?) {
        data.clear()
        data.addAll(t?: emptyList())
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener:(Int,Aty)->Unit){
        onItemClickListener = listener
    }

    override fun onClick(v: View?) {
        val holder = v?.getTag(R.id.tag_holder)
        if(holder is AtyViewHolder){
            val position = holder.adapterPosition
            onItemClickListener?.invoke(position, data[position])
        }
    }

}

class AtyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

    val textView: TextView = itemView.findViewById(R.id.textview)


}